<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <245369209@qq.com> 
// +----------------------------------------------------------------------

namespace app\basic\controller\api;
use app\common\controller\Api;
use think\response\Json;

/**
 * 危化品管理
 * @package app\Danger
 * @author LJQ<245369209@qq.com>
 */
class Danger extends Api

{

	protected $wfType = 3;
	
    /**
   *  危化品列表
   * @param string $equipeId 企业id,如果有只显示企业自己的信息.
   * @return Json
   */

	public function dangerList($scope = null){
       $where = [] ;
   
       $where['sourceType'] = ['eq',2] ;

       if($scope){
      
            $where['enterpriseId'] = ['in',$scope];
       }
		//分页参数
		if(($limit = input('limit')) && ($page = input('page'))){
			$startLimit =  ($page-1)*$limit;
			// 危化品列表
			$info =collection(model('Danger')->with('enterprise,staff,sensors,chemicals')->where($where)->limit($startLimit.','.$limit)->select())->toArray();
		}else{
			// 危化品列表
			$info =collection(model('Danger')->with('enterprise,staff,sensors,chemicals')->where($where)->select())->toArray();
		}
	   	 
	 	//总数
	   	 $count = model('Danger')->where($where)->count('id'); 
		
    		if ($count == 0) {
         			return json(['code'=>1,'msg'=>'没有找到危化品','count'=>$count,'data'=>$info]);
       		} 
       	
       		if ($count > 0){
       			return json(['code'=>1,'msg'=>'危化危化品','count'=>$count,'data'=>$info]);
       		}
            		return json(['code'=>0,'msg'=>'数据异常']);
   	 	 }
   	  
   	      //添加危化品
   	 public function add($data){
          
          if (empty($data)) {
              return json(['code'=>0,'msg'=>'数据异常,请检查']);
          }
		 $model = model('Basic/danger');
		 if(($data['sensorIds'])){
			 $sensor=$data['sensorIds'];
		 }
		 $result = $model->allowField(true)->save($data);
		 $id=$model->id;
		 //配置传感器
		 if($sensor){
			 if(model('Sensor')->where('dangerSourceId',$id)->select()){
				 model('Sensor')->where('dangerSourceId',$id)->update(['dangerSourceId'=>null]);//清空原来配置
			 }
			 model('Sensor')->where('id','in',$sensor)->update(['dangerSourceId'=>$id]);
		 }
		 setlog('dangeradd','danger',session('user_auth.id'));
		 if (false != $result) {
			 return json(['code'=>1,'msg'=>'危化品库添加成功']);
		 } else {
			 return json(['code'=>0,'msg'=>'危化品库添加失败,请检查']);
		 }
      }

   	

   /**
   * 危化品修改
   * @param  string DangerId 危化品id,
   * @return Json
   */
   	public function edit($data,$dangerId){
        
          if (empty($dangerId)) {
              return json(['code'=>0,'msg'=>'危化品不存在,请检查']);
          }
		//配置传感器
		if(!empty($data['sensorIds'][0])){

			model('Sensor')->where('dangerSourceId',$data['id'])->update(['dangerSourceId'=>null]);//清空原来配置
			if(model('Sensor')->where('id','in',$data['sensorIds'])->update(['dangerSourceId'=>$data['id']])){
				unset($data['sensorIds']);
			}else{
				return json(['code'=>0,'msg'=>'配置传感器失败']);
			}
		}
		$model = model('Basic/danger');
		if ( $model->allowField(true)->save($data,['id'=>$dangerId])) {
			setlog('dangerupdate','danger',session('user_auth.id'));
			return json(['code'=>1,'msg'=>'危化品库信息修改成功']);
		}else{
			return json(['code'=>0,'msg'=>'危化品库信息修改失败,请检查']);
		}
    }
    /**
   *  删除危化工艺
   * @param string DangerId 危化工艺id,
   * @param Json
   */
     
    public function del($dangerId) {
      	$dangerId = input('dangerId',0,'intval');
        $model = model('Basic/Danger');
      	if (empty ( $dangerId )||empty(($model->where(array('id'=>$dangerId))->find()))) {
           	return  json(['code'=>0,'msg'=>'危化品id不存在,请检查']);
        }
        if($model->destroy(['id'=>$dangerId]) !== FALSE){
            setlog('dangerdel','danger',session('user_auth.id'));
        	 return json(['code'=>1,'msg'=>'危化品删除成功']);
       }else{
         	 return $this->json(['code'=>0,'msg'=>'危化品删除失败,请检查']);
       }
    }

     
   /**
   *   查询危化品
   * @param keyword危化品名称
   * @return Json
   */
   	public function search($keyword=null,$enterprise=null){
            $where = [] ;
   
            $where['sourceType'] = ['eq',2] ;
		if($keyword){
			
			$where['name'] = ['like','%'.$keyword.'%'];
		}

            if ($enterprise) {
                   $where['enterpriseId'] = ['eq',$enterprise];
            }
   		//分页参数
		if(($limit = input('limit')) && ($page = input('page'))){
			$startLimit =  ($page-1)*$limit;
			//危化品列表
			$info =collection(model('Danger')->with('staff,sensors,chemicals')->where($where)->limit($startLimit.','.$limit)->select())->toArray();
		}else{
			//危化品列表
			$info =collection(model('Danger')->with('staff,sensors,chemicals')->where($where)->select())->toArray();
		}
	 	//总数
	       $count = model('Danger')->where($where)->count('id'); 
		 
    	if ($count == 0) {
              	return json(['code'=>0,'msg'=>'没有找到危化品','count'=>$count]);
       	} 
       	
       	if ($count > 0){
       		return json(['code'=>1,'msg'=>'危化品','count'=>$count,'data'=>$info]);
       	}
      }


	/**
	 * 获取危化品库位置
	 * @param string $enterpriseId
	 * @return Json
	 */
	public function getDangerPosition($craftId){
		$position = model('danger')->where('id',$dangerId)->value('latitudeLongitude');
		if($position){
			return json(['code'=>1,'msg'=>'','data'=>$position]);
		}else{
			return json(['code'=>0,'msg'=>'没有对应位置']);
		}
	}
      
      /**
       * 危化品入库
       * @param array $data 入库化学品
       * @param string $dangerId 危化品库ID
       */
      public function addChemical($data = array(),$dangerId = null){
      	$danger = model('Danger')->get($dangerId);
      	$chemicalId = $data['chemicalId'];
      	unset($data['chemicalId']);
      	if($danger->chemicals()->save($chemicalId,$data) != false){
      		setlog('dangerupdate','danger',session('user_auth.id'));
      		  return json(['code'=>1,'msg'=>'危化品入库成功']);
      	}
        return json(['code'=>0,'msg'=>'危化品入库失败']);
      	
      }
      
      /**
       * 更新危化品信息
       * @param $array $data 更新数据
       */
      public function editChemical($data = null){
      	if($data['dangerId'] == null || $data['id'] == null){
      		return json(['code'=>0,'msg'=>'更新失败']);
      	}
      	$danger = model('Danger')->get($data['dangerId']);
      	unset($data['dangerId']);
      	$chemicalId = $data['chemicalId'];
      	unset($data['chemicalId']);
      	if($danger->chemicals()->attach($chemicalId,$data) != false){
      		setlog('dangerupdate','danger',session('user_auth.id'));
      		return json(['code'=>1,'msg'=>'更新成功']);
      	}
      	return json(['code'=>0,'msg'=>'更新失败']);
      	
      }
      
      /**
       * 删除危化品信息
       * @param string $chemicalId
       * @param string $dangerId
       */
      public function delChemical($chemicalId = null,$dangerId = null){
      	if($chemicalId&&$dangerId){
      		$danger = model('Danger')->get($dangerId);
      		$danger->chemicals()->detach($chemicalId);
      		return json(['code'=>1,'msg'=>'删除成功']);
      	}
      	return json(['code'=>0,'msg'=>'删除失败']);
      }
      
      /**
       * 申请审核
       * @param string $hazardId
       */
      public function audit($dangerId = null){
      	$workflow = model('Workflow')->where('wfType',$this->wfType)->find();
      	if(empty($workflow)||empty($workflow->steps)){
      		return json(['code'=>0,'msg'=>'无对应审核流程','count'=>'','data'=>'']);
      	}else{
      	$danger = model('Danger')->get($dangerId);
	      	if($danger->isUpdate(true)->save(['checkStatus'=>2]) !== false){
	      		$data['title'] = $danger['name'];
	      		$data['wfFlowInfoId'] = $workflow['id'];
	      		$data['wfType'] = $this->wfType;
	      		$data['busiId'] = $dangerId;
	      		$data['enterpriseId'] = $danger['enterpriseId'];
	      		$data['applyPerson'] = session('user_auth.id');
	      		if(model('Wfitem')->save($data)){
	      			return json(['code'=>1,'msg'=>'申请成功','count'=>'','data'=>'']);
	      		}else{
	      			return json(['code'=>0,'msg'=>'申请失败','count'=>'','data'=>'']);
	      		}
	      	}
      	}
      	return json(['code'=>0,'msg'=>'申请失败','count'=>'','data'=>'']);
      	 
      }


}